package com.pi.base.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.pi.base.common.BasePage;
import com.pi.base.common.BaseResult;
import com.pi.base.entity.SysGroup;
import com.pi.base.entity.SysMenu;
import com.pi.base.entity.SysUser;
import com.pi.base.service.ISysGroupService;
import com.pi.base.service.ISysUserService;
import com.pi.base.util.JsonTool;
import com.pi.base.util.SysAuthUtil;
import com.pi.base.util.ValidUtil;
import com.pi.base.vo.SysGroupVO;

@Controller
@RequestMapping("/usergroup")
public class UserGroupController  extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(UserGroupController.class);
	@Resource
	private ISysUserService sysUserService;
	@Resource
	private ISysGroupService sysGroupService;
	
	@RequestMapping(value="/getGroups",method={RequestMethod.POST})
	@ResponseBody
	public String getGroups(HttpSession httpSession) throws Exception{
		String groupId = "1";
		SysUser loginedUser = this.getLoginedUserInfo(httpSession);
		if(!isSupperAdmin(loginedUser)){
			groupId = loginedUser.getGroupid().toString();
		}
		List<SysGroup> groupList = sysUserService.getGroupWithSubListByGroupId(groupId);
		List<SysGroup> newList_ = SysAuthUtil.turnListToTreeList(groupList, SysGroup.class, "getId", groupId, "getId", "getParentid", "setSubList");
		String jsonStr = JSONObject.toJSONString(newList_);
		jsonStr = jsonStr.replaceAll("subList", "children");
		return jsonStr;
	}
	
	@RequestMapping(value="/getGroupList",method={RequestMethod.POST})
	@ResponseBody
	public String getGroupList(HttpSession httpSession) throws Exception{
		String groupId = "1";
		SysUser loginedUser = this.getLoginedUserInfo(httpSession);
		if(!isSupperAdmin(loginedUser)){
			groupId = loginedUser.getGroupid().toString();
		}
		List<SysGroupVO> groupList = sysUserService.getGroupWithSubListByGroupId2(groupId);
		List<SysGroupVO> newList_ = SysAuthUtil.turnListToTreeList(groupList, SysGroupVO.class, "getId", groupId, "getId", "getParentid", "setChildren");
		String jsonStr = JSONObject.toJSONString(newList_);
		jsonStr = jsonStr.replaceAll("subList", "children");
		return jsonStr;
	}

	@RequestMapping(value="/getUserListByGroupId",method={RequestMethod.POST})
	@ResponseBody
	public String getUserListByGroupId(HttpSession httpSession,@RequestParam Integer groupId,@RequestParam(required=false) Integer page,
			@RequestParam(required=false) Integer rows) throws Exception{
		page = (page == null) ? 1 : page;
		rows = (rows == null) ? 0 : rows; //默认查询全部
		BasePage<SysUser> userListPage = sysGroupService.listUserByGroupId(groupId, page, rows);
		return JsonTool.toJsonString(userListPage);
	}
	
	@RequestMapping(value="/doGroupAdd",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doGroupAdd(@ModelAttribute SysGroup newGroup,HttpSession httpSession) throws Exception{
		logger.info(newGroup.toString());
		if(newGroup == null || ValidUtil.isBlank(newGroup.getName()) 
				|| ValidUtil.isBlank(newGroup.getCode())
	            /*|| newGroup.getChildstate() == null*/){ 		
			return BaseResult.newErrorResult("参数不完整");
		}
		//验证code是否重复
		if(sysGroupService.countGroupCode(null,newGroup.getCode()) > 0){
			return BaseResult.newErrorResult("分组编码已经存在，请重新输入");
		}
		newGroup.setState(0);
		newGroup.setCreatetime(new Date());
		newGroup.setCreatername(this.getLoginedUserInfo(httpSession).getUsername());
		sysGroupService.insertSysGroup(newGroup);
		return BaseResult.newSuccessResult("新增成功");
	}
	
	@RequestMapping(value="/doGroupUpdate",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doGroupUpdate(@ModelAttribute SysGroup updateGroup) throws Exception{
		logger.info(updateGroup.toString());
		if(updateGroup == null || updateGroup.getId() == null || ValidUtil.isBlank(updateGroup.getName()) 
				|| ValidUtil.isBlank(updateGroup.getCode())
	            /*|| updateGroup.getChildstate() == null*/){ 		
			return BaseResult.newErrorResult("参数不完整");
		}
		//验证code是否重复
		if(sysGroupService.countGroupCode(updateGroup.getId(),updateGroup.getCode()) > 0){
			return BaseResult.newErrorResult("分组编码已经存在，请重新输入");
		}
		return sysGroupService.updateSysGroup(updateGroup);
	}
	
	@RequestMapping(value="/doGroupRemove",method={RequestMethod.POST})
	@ResponseBody
	public BaseResult doGroupRemove(@RequestParam Integer groupId) throws Exception{
		return sysGroupService.deleteGroup(groupId);
	}
}
